package com.mediacloud.app.newsmodule.fragment.baoliao.adapter;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapterX;
import com.mediacloud.app.newsmodule.fragment.SimpleBottomSheetDialog;
import com.mediacloud.app.newsmodule.fragment.baoliao.XPlayerVideoListPlayerUtil;
import com.mediacloud.app.newsmodule.fragment.baoliao.holder.BaoLiaoNavListImgVideoHolder;
import com.mediacloud.app.newsmodule.fragment.baoliao.holder.BaoLiaoNavListPureTxtHolder;
import com.mediacloud.app.newsmodule.model.baoliao.list.list.BaoNiaoListItem;
import com.mediacloud.app.newsmodule.utils.BaoLiaoItemMoreHandler;
import com.mediacloud.app.newsmodule.utils.GridLayoutSpanCountUtils;
import com.mediacloud.app.newsmodule.utils.spider.SpiderObserver;


/***
 * 爆料导航列表项
 */
public class BaoLiaoNavListAdapter extends BaseRecyclerAdapterX<BaoNiaoListItem> implements SpiderObserver {
    public SimpleBottomSheetDialog.ItemClickListener simpleBtmItemClickListener;
    public XPlayerVideoListPlayerUtil videoListPlayerUtil;

    public Object saveTag;

    /**
     * 是否按照资讯导航的样式显示
     * */
    private boolean isNewsStyle = false;

    public BaoLiaoNavListAdapter(Context context, View recyclerView) {
        super(context);
        videoListPlayerUtil = new XPlayerVideoListPlayerUtil(context, recyclerView);
    }

    public void setNewsStyle(boolean newsStyle) {
        isNewsStyle = newsStyle;
    }


    public void stopPlay(){
        if (videoListPlayerUtil!=null){
            videoListPlayerUtil.stopPlay();
        }
    }

    public void playResume(){
        if (videoListPlayerUtil!=null){
            videoListPlayerUtil.getPlayer().resume();
        }
    }

    @Override
    public void destroy() {
        if (videoListPlayerUtil != null) {
            videoListPlayerUtil.onDestroy();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.w("BaoLiaoNavListAdapter", "onCreateViewHolder");
        BaoLiaoNavListPureTxtHolder baoLiaoNavListPureTxtHolder;
        int viewModel = GridLayoutSpanCountUtils.ItemTypeSpec.getMode(viewType);
        viewType = GridLayoutSpanCountUtils.ItemTypeSpec.getSize(viewType);
       /* if(viewType == NEWS){
            baoLiaoNavListPureTxtHolder = new BaoLiaoNavListNewsItemVideoHolder(LayoutInflater.from(getContext()).inflate(R.layout.baoniao_navlist_news_item, parent, false));
        } else */
            if (viewType == ALL) {
            baoLiaoNavListPureTxtHolder = new BaoLiaoNavListImgVideoHolder(LayoutInflater.from(getContext()).inflate(R.layout.baoniao_navlist_videoimgitem, parent, false), viewModel);
        } else {
            baoLiaoNavListPureTxtHolder = new BaoLiaoNavListPureTxtHolder(LayoutInflater.from(getContext()).inflate(R.layout.baoniao_navlist_purtextitem, parent, false));
        }
        baoLiaoNavListPureTxtHolder.moreIcon.setTag(baoLiaoNavListPureTxtHolder);
        baoLiaoNavListPureTxtHolder.moreIcon.setOnClickListener(this);
        baoLiaoNavListPureTxtHolder.setVideoListPlayerUtil(videoListPlayerUtil);
        if(baoLiaoNavListPureTxtHolder.moreIcon2!=null){
            baoLiaoNavListPureTxtHolder.moreIcon2.setTag(baoLiaoNavListPureTxtHolder);
            baoLiaoNavListPureTxtHolder.moreIcon2.setOnClickListener(this);
        }
        return baoLiaoNavListPureTxtHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        BaoLiaoListSize = getData().size();
        Log.w("BaoLiaoNavListAdapter", "onBindViewHolder");
        super.onBindViewHolder(holder, position);
        View baoliaobiaoti = ((BaoLiaoNavListPureTxtHolder) holder).baoniaobiaoti;
        if(baoliaobiaoti != null){
            baoliaobiaoti.setTag(R.id.recyclerItemTag, position);
            if (!baoliaobiaoti.hasOnClickListeners()) {
                baoliaobiaoti.setOnClickListener(this);
            }
        }
        IBaoLiaoListHolder bao = (IBaoLiaoListHolder) holder;
        bao.updateData(getItem(position),position);
    }

    public int currentClickMorePosition = -1;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        BaoLiaoNavListPureTxtHolder baoLiaoNavListPureTxtHolder = v.getTag() instanceof BaoLiaoNavListPureTxtHolder ? (BaoLiaoNavListPureTxtHolder) v.getTag() : null;
        if (baoLiaoNavListPureTxtHolder != null) {
            Object tag = baoLiaoNavListPureTxtHolder.itemView.getTag(R.id.recyclerItemTag);
            if (tag == null)
                return;
            int position = tag != null && tag instanceof Integer ? (int) tag : -1;
            currentClickMorePosition = position;
            BaoLiaoItemMoreHandler.showMoreAction(v.getContext(), simpleBtmItemClickListener,getItem(position));
        }
    }

    @Override
    public int getItemViewType(int position) {
       /* if(isNewsStyle){
            return GridLayoutSpanCountUtils.ItemTypeSpec.makeItemTypeSpec(GridLayoutSpanCountUtils.ItemTypeSpec.MODEL_NO_ONE, NEWS);
        }*/
        BaoNiaoListItem obk = getItem(position);
        if (obk.getTipoffinfoList() != null && obk.getTipoffinfoList().size() > 0 || obk.getShortTitleStyle() != null && obk.getShortTitleStyle().size() > 0) {
            int size = obk.baoNiaoMediaList == null ? 0 : obk.baoNiaoMediaList.size();
            return GridLayoutSpanCountUtils.ItemTypeSpec.makeItemTypeSpec(size, ALL);
        }
        return GridLayoutSpanCountUtils.ItemTypeSpec.makeItemTypeSpec(GridLayoutSpanCountUtils.ItemTypeSpec.MODEL_NO_ONE, TXT);
    }


    /**
     * 1 纯文本
     * 2 图文 视频之类的
     * 3 资讯导航的样式
     */
    public static final int TXT = 1, ALL = 2, NEWS = 3;
    public static final int BaoLiaoListSize=0;


    public interface IBaoLiaoListHolder {
        void updateData(BaoNiaoListItem item,int position);
    }
}